package com.situ.layuimini.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.layuimini.entity.User;
import com.situ.layuimini.mapper.UserMapper;
import com.situ.layuimini.service.IUserService;
import com.situ.layuimini.util.TokenUtil;
import com.situ.layuimini.vo.LayuiTableVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author kkkkkk
 * @since 2022-05-30
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public String login(String username, String password) {
        User user = baseMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));
        String token = null;
        if (user != null && user.getPassword().equals(password)) {
            token = TokenUtil.token(user);
            redisTemplate.opsForValue().set("USERID-" + user.getId(), user);
            //redisTemplate.opsForValue().set("USERID-"+user.getId(),user,1, TimeUnit.HOURS);
        }
        return token;
    }

    @Override
    public LayuiTableVO selectList(Integer page, Integer limit) {
        LayuiTableVO vo = new LayuiTableVO();
        PageHelper.startPage(page, limit);
        List<User> list = baseMapper.selectList(null);
        vo.setData(list);
        vo.setCount(new PageInfo<>(list).getTotal());
        return vo;
    }
}
